package com.gameObjects;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import android.util.Log;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.gl.SpriteBatcher;
import com.badlogic.androidgames.framework.impl.GLGame;
import com.badlogic.androidgames.framework.math.OverlapTester;
import com.badlogic.androidgames.framework.math.OverlapTester2;
import com.baseinfinityrun.Assets;
import com.baseinfinityrun.GameScreen;

public class Present extends Era
{
	PresentPlayer player;
	Obstacle ground;
	boolean collision;
	
	public Present(PresentPlayer player, PresentBG bg, Game game)
	{
		super(player, bg, game);
		this.player = player;
		
		try
		{
			stream = game.getFileIO().readAsset("present.xml");
		}
		
		catch(IOException e){}
				
		LoadObstacle(stream);
		collision = false;
		actualEraID = 5;
	}

	public void Update(float deltaTime) 
	{
		ChangeBg();
		Touch();
		this.player.Update(deltaTime);
		CollisionObstacles();
	}

	@Override
	public void Draw(SpriteBatcher batcher)
	{
		lastBg.Draw(batcher);
		actualBg.Draw(batcher);		
		player.Draw(batcher);
		for(Obstacle obs : obstacles)
		{
			obs.Draw(batcher);
		}
	}

	@Override
	public String CollisionObstacles() 
	{
		collision = false;
		for(Obstacle ob : this.obstacles)//passa pelos obstaculos
		{

				if(OverlapTester2.rightCollision(player.rectCollider, ob.rectCollider))
				{
					Log.d("testes", "TuCH TUCH QUERO VER!");
					return "obstacle";
				}
				
				if(OverlapTester2.downCollision(player.rectCollider, ob.rectCollider) && player.GetState() != "onplatform")
				{
					player.SetState("onplatform");
					collision = true;
					player.position.y = ob.rectCollider.height + ob.rectCollider.lowerLeft.y + player.rectCollider.height * 0.48f;
				}
				
				
					/*player.position.y = ob.rectCollider.height + ob.rectCollider.lowerLeft.y + player.rectCollider.height * 0.49f;
					
					if(player.rectCollider.lowerLeft.y > ob.position.y + ob.rectCollider.height * 0.4f 
					&& player.rectCollider.lowerLeft.y < ob.position.y + ob.rectCollider.height * 0.6f)
					{
						collision = true;

						if(player.GetState() != "onplatform")
						{
							player.SetState("onplatform");
							player.position.y = ob.rectCollider.height * 0.8f + ob.rectCollider.lowerLeft.y + player.rectCollider.height * 0.3f;
						}
					}
					
					else
					{*/
						
					//}
		}	
		
		if(!collision && player.GetState() == "onplatform")
		{
			player.SetState("jumping");
		}
		
		return "not";
	}
}
